<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>实验室助理工作管理系统</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
    <meta name="description" content="LightYear是一个基于Bootstrap v3.3.7的后台管理系统的HTML模板。">
    <meta name="author" content="yinqi">
    <link rel="stylesheet" th:href="@{/Light-Year-Admin/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/Light-Year-Admin/css/materialdesignicons.min.css}">
    <link rel="stylesheet" th:href="@{/Light-Year-Admin/css/style.min.css}">
    <!--对话框-->
    <link rel="stylesheet" th:href="@{/Light-Year-Admin/js/jconfirm/jquery-confirm.min.css}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>
<body>

<!--页面主要内容-->
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header"><h4>工作内容信息列表</h4></div>
            <div class="card-header">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-7">
                            <form class="form-inline" action="" >
                                <div class="form-group">
                                    <label class="sr-only" for="example-if-email">搜索</label>
                                    <input class="form-control" type="text" id="example-if-email" name="name" placeholder="search..">
                                </div>
                                <div class="form-group">
                                    <button class="btn btn-default" type="submit">搜索</button>
                                </div>
                            </form>
                        </div>
                        <div class="col-xs-4">
                            <button class="btn btn-primary m-r-5" id="add"><i class="mdi mdi-plus"></i>填写工作内容</button>
                        </div>
                        <div class="col-xs-1">
                            <a class="btn btn-yellow m-r-5" href="/toAssistant"><i class="mdi mdi-refresh"></i>刷新</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <table class="table table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>工作内容编号</th>
                        <th>周次</th>
                        <th>时间</th>
                        <th>工作内容</th>
                        <th>助理编号</th>
                        <th>助理姓名</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="tb_teacher">
                    </tbody>
                </table>
            </div>

            <!-- 模态框 -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">添加或修改信息</h4>
                        </div>
                        <div class="modal-body">

                            <!--页面主要内容-->
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="card">
                                        <div class="card-body">
                                            <form action=""  class="row" id="userForm" name="myform">
                                                <input id="isEdit" type="text" hidden>
                                                <div class="form-group col-md-12">
                                                    <label for="jobContentId">工作内容编号</label>
                                                    <input type="text" class="form-control" id="jobContentId" name="jobContentId"  value="" placeholder="请输入工作内容编号" required/>
                                                </div>
                                                <div class="form-group col-md-12">
                                                    <label for="timesWeek">周次</label>
                                                    <input type="text" class="form-control" id="timesWeek" name="timesWeek" value="" placeholder="请输入周次" required/>
                                                </div>
                                                <div class="form-group col-md-12">
                                                    <label for="jobContent">工作内容</label>
                                                    <input type="text" class="form-control" id="jobContent" name="jobContent"  value="" placeholder="请输入工作内容" required/>
                                                </div>
                                                <div class="form-group col-md-12">
                                                    <label>时间</label>
                                                    <input type="text" class="layui-input" id="jobTime" name="jobTime" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" required>
                                                </div>
                                                <div class="form-group col-md-12">
                                                    <label for="assistantId">助理编号</label>
                                                    <input type="text" class="form-control" id="assistantId" name="assistantId"  value="" disabled="true" placeholder="请输入助理编号" required/>
                                                </div>
                                                <div class="form-group col-md-12">
                                                    <label for="stuName">助理姓名</label>
                                                    <input type="text" class="form-control" id="stuName" name="stuName"  value="" disabled="true" placeholder="请输入助理编号" required/>
                                                </div>
                                            </form>

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--End 页面主要内容-->
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" onclick="modify()">提交</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--End 页面主要内容-->

<script type="text/javascript" th:src="@{/js/jquery-3.6.0.min.js}"></script>
<script type="text/javascript" th:src="@{/Light-Year-Admin/js/bootstrap.min.js}"></script>
<script type="text/javascript" th:src="@{/Light-Year-Admin/js/perfect-scrollbar.min.js}"></script>
<script type="text/javascript" th:src="@{/Light-Year-Admin/js/main.min.js}"></script>
<!--消息提示-->
<script th:src="@{/Light-Year-Admin/js/bootstrap-notify.min.js}"></script>
<script type="text/javascript" th:src="@{/Light-Year-Admin/js/lightyear.js}"></script>
<!--对话框-->
<script th:src="@{/Light-Year-Admin/js/jconfirm/jquery-confirm.min.js}"></script>
<!-- layui -->
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>

<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        //日期时间选择器
        laydate.render({
            elem: '#jobTime'
            ,type: 'datetime'
        });
    });
</script>

<script type="text/javascript">

    var userId = "";
    var userName = "";
    var assistantId1 =""
    var perms = "";

    $(function () {
        // 获取session
        $(function () {
            // 获取session（获取登录者的id）
            $.ajax({
                "url" : "getUserSession",
                "method" : "POST",
                "cache" : false,
                "async" : false,
                success: function (result) {
                    if (result.code == 200){
                        userId = result.id;
                        userName = result.name;
                        assistantId1 = result.assistantId1;
                        perms = result.perms;
                        $("#name1").html(perms+"："+userName);
                    }
                }
            });

            // 查询所有数据
            getDepartment();
        });

        // 查询所有数据
        getDepartment();

        // 打开新增页面
        $("#add").click(function () {
            // 清空模态框表单
            reset();
            $('#assistantId').val(assistantId1);
            $('#stuName').val(userName);
            // 打开模态框
            $("#myModalLabel").html("填写工作内容");
            $('#myModal').modal('show');
        });
    });

    // 查询所有数据
    function getDepartment() {
        $.ajax({
                type: "GET",
                url: "/jobContent/selectAll/" + assistantId1,
                contentType: 'application/json;charset=utf-8', //设置请求头信息
                dataType: "json",
                success: function (result) {
                    let html = "";
                    //遍历结果集result,
                    for(let i = 0;i<result.length;i++){
                        //一次取出teacher下的每一个变量m,并添加到html中
                        html += "<tr>"+
                            "<td>"+result[i].jobContentId+"</td>"+
                            '<td>'+result[i].timesWeek+"</td>"+
                            "<td>"+result[i].jobTime+"</td>"+
                            "<td>"+result[i].jobContent+"</td>"+
                            "<td>"+assistantId1+"</td>"+
                            "<td>"+userName+"</td>"+
                            '<td>'+'<a class="btn btn-info btn-sm" role="button" href="javascript:edit(\''+result[i].jobContentId+'\')"><i class="mdi mdi-pencil"></i>修改'+
                            '</a></td>'
                            +"</tr>";
                    }
                    //将html标签拼接在html中
                    $("#tb_teacher").html(html);
                }
            }
        )
    }

    // 删除
    function del(depId) {
        console.log(depId)
        $.confirm({
            title: '提示框',
            content: '是否删除？',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function(){
                        lightyear.loading('show');  // 显示
                        $.ajax({
                            type: "GET",
                            url: "/department/delete?deptId=" + depId,
                            contentType: 'application/json;charset=utf-8', //设置请求头信息
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    setTimeout(function () {
                                        lightyear.loading('hide');
                                        lightyear.notify('删除成功', 'danger', 5000, 'mdi mdi-emoticon-happy', 'top', 'center');
                                        //window.location.href = "/toDepartment";
                                        getDepartment()
                                    }, 600);
                                }
                            },
                            error: function (result) {
                                errorToast(result.msg);
                            }
                        });
                    }
                },
                cancel: {
                    text: '关闭',
                    action: function(){

                    }
                },
            }
        });
    }

    // 新增or修改
    function modify() {
        //获取模态框数据
        var jobContentId = $("#jobContentId").val();
        var timesWeek = $("#timesWeek").val();
        var jobTime = $("#jobTime").val();
        var assistantId = $("#assistantId").val();
        var jobContent = $("#jobContent").val();

        var param = {jobContentId: jobContentId, timesWeek: timesWeek, jobTime: jobTime, assistantId: assistantId,jobContent:jobContent};
        console.log(param)
        //  判断是否是修改（1是修改）
        var isEdit = $('#isEdit').val();
        var url = '';
        if (isEdit == 1){
            url = '/jobContent/update';
        }else {
            url = '/jobContent/add';
        }

        $.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(param),
            success: function (data) {
                if(data.code == 200 ){
                    reset();
                    $('#myModal').modal('hide');
                    getDepartment();
                    lightyear.notify('操作成功', 'success', 500, 'mdi mdi-emoticon-happy', 'top', 'center');
                }else{
                    reset();
                    $('#myModal').modal('hide');
                    getDepartment();
                    lightyear.notify('操作失败，工作内容编号不能重复', 'danger', 500, 'mdi mdi-emoticon-happy', 'top', 'center');
                }
            }
        });
    }

    // 打开修改页面
    function edit(jobContentId) {
        $.ajax({
            url: "/jobContent/findOne/" + jobContentId,
            type: "GET",
            success: function (result) {
                if (result.data) {
                    //向模态框中传值
                    $('#isEdit').val("1"); // 判断是否是修改
                    $('#jobContentId').val(result.data.jobContentId);
                    $('#timesWeek').val(result.data.timesWeek);
                    $('#jobTime').val(result.data.jobTime);
                    $('#assistantId').val(result.data.assistantId);
                    $('#jobContent').val(result.data.jobContent);
                } else {
                    alert(result.data.message);
                }
            }
        });
        $("#myModalLabel").html("修改工作内容");
        $('#myModal').modal('show');
    }

    // 清空模态框
    function reset() {
        $('#isEdit').val("");
        $("#jobContentId").val("");
        $("#timesWeek").val("");
        $("#jobTime").val("");
        $("#assistantId").val("");
        $("#jobContent").val("");
    }

</script>
</body>
</html>